//
//  YQChat.swift
//  yq微信
//
//  Created by 杨庆 on 2018/1/12.
//  Copyright © 2018年 yq. All rights reserved.
//

import UIKit

/// 聊天界面消息 -- 数据模型
class YQChat: NSObject {

    /// 聊天文字
    var contentText:String?
    
    /// 文字背景图片
    var contentTextBGImage:UIImage?
    
    /// 文字背景高亮图片
    var contentTextBGHLImage:UIImage?
    
    /// 头像地址
    var userIconStr:String?
    
    /// 时间
    var timeStr:String?
    
    /// 是我还是对方(它)
    var me:Bool?
   
    /*
     * emsg属性是cell逻辑处理部分传递过来的数据
     */
    
    /// 上次消息时间
    var preTimestamp : Int64?
   
    /// 是否显示时间
    var showTime:Bool?
    
    // (聊天消息)数据
    var emsg : EMMessage? {
      
        didSet{
            let userLogin : String = EMClient.shared().currentUsername

            // 发送方
            if userLogin == emsg?.from {
            
                self.me = true // 从数据判断是对方还是我
                self.userIconStr = "xhr"
                self.contentTextBGImage = UIImage(named: "SenderTextNodeBkg")
                self.contentTextBGHLImage = UIImage(named:"SenderTextNodeBkgHL")
            
            }else {
                self.me = false
                self.userIconStr = "add_friend_icon_offical"
                self.contentTextBGImage = UIImage(named:"ReceiverTextNodeBkg")
                self.contentTextBGHLImage = UIImage(named:"ReceiverTextNodeBkgHL")
            }
            
            //TODO: 时间的逻辑处理
            // 60000代表时间差为一分钟 才进行显示
            self.showTime = abs((emsg?.timestamp)! - self.preTimestamp!)  > 60000
           
            // 时间值
            self.timeStr = String.yq_convastionTimeStr(time: Double((emsg?.timestamp)!))
            
            // 解析消息
            let msgBody:EMMessageBody = (emsg?.body)!
         
            switch msgBody.type {

            case EMMessageBodyTypeText:
                // 收到文字消息
                let txt:String? = (msgBody as! EMTextMessageBody).text
                print("收到的文字是:\(txt!)")
                self.contentText = txt
          
            case EMMessageBodyTypeImage:
                // 收到的是图片消息
                let body:EMImageMessageBody = (msgBody as! EMImageMessageBody)
                print("缩略图服务器路径 --\(body.thumbnailRemotePath)")
                print("缩略图本地路径 --\(body.thumbnailLocalPath)")
                print("缩略图的显示名称 --\(body.thumbnailDisplayName)")
                
            case EMMessageBodyTypeLocation:
                // 收到的是位置消息
                let body :EMLocationMessageBody = msgBody as! EMLocationMessageBody
                print("地址信息 --\(body.address)")
                print("纬度 --\(body.latitude)")
                print("经度 --\(body.longitude)")
                
            case EMMessageBodyTypeVoice:
                let body:EMVoiceMessageBody = msgBody as! EMVoiceMessageBody
                print("语音消息时长:\(body.duration)秒")

            default:
                print("收到的是未知消息...")
            }
        }
    }
    
    required override init() {
        super.init()
    }
    
    // 快速创建chat对象
    class func createObjectChatWith( _ preTime:Int64,_ emsg:EMMessage)->YQChat{
        let chat:YQChat = self.init()
        chat.preTimestamp = preTime
        chat.emsg = emsg
        return chat
    }


}
